package com.wlts.wzy.KSOrderModule.util;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryLogisticsRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryLogisticsResponse;
import com.wlts.wzy.KSOrderModule.entity.KsExpressCode;
import com.wlts.wzy.KSOrderModule.entity.KsOrders;
import com.wlts.wzy.KSOrderModule.service.IKsExpressCodeService;
import com.wlts.wzy.KSOrderModule.service.IKsOrdersService;
import com.wlts.wzy.KSUserModule.entity.KsStoreUser;
import com.wlts.wzy.KSUserModule.service.IKsStoreUserService;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.wlts.wzy.configBean.LmConfig.LmConfig.client;

@Component
public class LmExpressToLocalUtil {
    @Autowired
    private IKsOrdersService ksOrdersService;
    @Autowired
    private IKsStoreUserService ksStoreUserService;
    @Autowired
    private IKsExpressCodeService ksExpressCodeService;

    /**
     * 获得发货订单的快递单号
     */
    @Scheduled(fixedDelay = 120 * 1000)
    public void LmUpdateOrdersToLocal(){

        MyLambdaQueryWrapper<KsStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(KsStoreUser::getDelFlag, "0");

        userWrapper.isNotNull(KsStoreUser::getKsSellerId);

        List<KsStoreUser> ksOauthList = ksStoreUserService.list(userWrapper);

        for (KsStoreUser user : ksOauthList
        ) {
            MyLambdaQueryWrapper<KsOrders> queryWrapper = new MyLambdaQueryWrapper<>();

            queryWrapper.in(KsOrders::getSelfOrder, 1);//平台商品的订单 0未确定 1是 2不是

            queryWrapper.eq(KsOrders::getPutLmStatus, 1);//推送lm状态 0未推送 1已推送 2推送失败

            queryWrapper.eq(KsOrders::getSendStatus, 1);//lm发货状态 0发货 1已发货

            queryWrapper.eq(KsOrders::getStatus, 30);

            queryWrapper.isNull(KsOrders::getExpressNo);

            queryWrapper.eq(KsOrders::getSellerId, user.getKsSellerId());

            queryWrapper.isNotNull(KsOrders::getLmOrderId);

            int page = 1;
            int pageSize = 15;

            while (true) {

                List<KsOrders> ksOrdersList = ksOrdersService.page(queryWrapper, page, pageSize, true).getRecords();
//                System.out.println("这是需要更新发货信息的订单-----");
//                System.out.println(ksOrdersList.toString());
//                System.out.println("这是需要更新发货信息的订单-----");
                ksOrdersList.forEach(ksOrders -> {
                    QueryLogisticsRequest request = new QueryLogisticsRequest();
                    request.setBizId(LmConfig.LZ_BIZ_ID);
                    request.setAccountType("ANONY");
                    request.setThirdPartyUserId(user.getKsSellerId() + "");
                    request.setBizUid(request.getThirdPartyUserId());
                    request.setLmOrderId(Long.valueOf(ksOrders.getLmOrderId()));

                    try {
                        QueryLogisticsResponse response = client.getAcsResponse(request);



                        if (response.getCode().equals("SUCCESS")) {
                            List<QueryLogisticsResponse.DataItem> data = response.getData();
                            QueryLogisticsResponse.DataItem item = data.get(0);
                            if (item != null) {
                                ksOrders.setExpressNo(item.getMailNo());
                                ksOrders.setExpressName( item.getLogisticsCompanyName());
                                KsExpressCode expressCode = ksExpressCodeService.getOne(new MyLambdaQueryWrapper<KsExpressCode>().like(KsExpressCode::getName, item.getLogisticsCompanyName()));
                                if (expressCode != null) {
                                    ksOrders.setExpressCode(expressCode.getCode());
                                }
                            }
                            ksOrdersService.updateById(ksOrders);
                        }
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                });
                if (ksOrdersList.size() < pageSize) {
                    break;
                }
                page += 1;
            }
        }

    }


}
